package com.huitone.smspfm.oldclient.core;

import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;

import com.huitone.smspfm.oldclient.asclient.resp.RespFileWriter;
import com.huitone.smspfm.oldclient.asclient.resp.RespFileWriterForMultiAccount;
import com.huitone.smspfm.oldclient.asserver.recv.RecvFileWriter;
import com.huitone.smspfm.oldclient.asserver.recv.RecvFileWriterForMultiAccount;
import com.huitone.smspfm.oldclient.asserver.report.ReportFileWriter;
import com.huitone.smspfm.oldclient.asserver.report.ReportFileWriterForMultiAccount;
import com.huitone.smspfm.oldclient.logger.ExtLogger;

import com.huitone.smspfm.oldclient.model.AbstractSms;
import com.huitone.smspfm.oldclient.model.RecvSms;
import com.huitone.smspfm.oldclient.model.ReportSms;
import com.huitone.smspfm.oldclient.model.RespSms;

/**   
* @Title: FileWriterDisparther.java 
* @Package com.unicom.gddw.module 
* @Description: TODO(用一句话描述该文件做什么) 
* @author ybliang   
* @date 2017年3月21日 下午4:06:15 
* @version V1.0   
*/
public class FileWriterDispatcher implements FileWriter {
	
	private final static ExtLogger logger = ExtLogger.create(FileWriterDispatcher.class);
	
	/**
	 * --store
	 *     |
	 *     ---recv
	 *     |    |
	 *     |    ---[clientname]
	 *     |    |
	 *     |    ...[clientname](N)
	 *     |
	 *     ---report
     *     |    |
	 *     |    ---[clientname]
	 *     |    |
	 *     |    ...[clientname](N)
	 *     |
	 *     ---resp
     *     |    |
	 *     |    ---[clientname]
	 *     |    |
	 *     |    ...[clientname](N)
	 */
	private Map<String, Map<String, FileWriter>> store = new ConcurrentHashMap<String, Map<String, FileWriter>>();
	
	private boolean isMultiModel = false;

	public synchronized void dispatcher(AbstractSms abstractSms) throws IOException {
		
		if (abstractSms == null) {
			return;
		}
		
		if (isMultiModel) {
			
			String clientname = abstractSms.getClientname();
			
			if (abstractSms instanceof RecvSms) {
				
				if (!store.containsKey("recv")) {
					store.put("recv", new ConcurrentHashMap<>());
				}
				
				if (!store.get("recv").containsKey(clientname)) {
					store.get("recv").put(clientname, new RecvFileWriterForMultiAccount());
				}
				
				store.get("recv").get(clientname).write(abstractSms);
				
			} else if (abstractSms instanceof ReportSms) {
				
				if (!store.containsKey("report")) {
					store.put("report", new ConcurrentHashMap<>());
				}
				
				if (!store.get("report").containsKey(clientname)) {
					store.get("report").put(clientname, new ReportFileWriterForMultiAccount());
				}
				
				store.get("report").get(clientname).write(abstractSms);
				
			} else if (abstractSms instanceof RespSms) {
				
				if (!store.containsKey("resp")) {
					store.put("resp", new ConcurrentHashMap<>());
				}
				
				if (!store.get("resp").containsKey(clientname)) {
					store.get("resp").put(clientname, new RespFileWriterForMultiAccount());
				}
				
				store.get("resp").get(clientname).write(abstractSms);
			} else {
				logger.warn("找不到对应的写文件处理器:" + abstractSms.toString());
			}
		} else {
			
			String key = null;
			
			if (abstractSms instanceof RespSms) {
				key = RespSms.class.getName();
				if (!store.containsKey(key)) {
					store.put(key, new ConcurrentHashMap<>());
				}
				if (!store.get(key).containsKey(key)) {
					store.get(key).put(key, new RespFileWriter());
				}
				store.get(key).get(key).write(abstractSms);
			} else if (abstractSms instanceof RecvSms) {
				key = RecvSms.class.getName();
				if (!store.containsKey(key)) {
					store.put(key, new ConcurrentHashMap<>());
				}
				if (!store.get(key).containsKey(key)) {
					store.get(key).put(key, new RecvFileWriter());
				}
				store.get(key).get(key).write(abstractSms);
			} else if (abstractSms instanceof ReportSms) {
				key = ReportSms.class.getName();
				if (!store.containsKey(key)) {
					store.put(key, new ConcurrentHashMap<>());
				}
				if (!store.get(key).containsKey(key)) {
					store.get(key).put(key, new ReportFileWriter());
				}
				store.get(key).get(key).write(abstractSms);
			} else {
				logger.warn("找不到对应的写文件处理器:" + abstractSms.toString());
			}
		}
	}
	
	public void destroy() {
		
		if (store != null) {
			Iterator<Entry<String, Map<String, FileWriter>>> iterator = store.entrySet().iterator();
			while(iterator.hasNext()) {
				Entry<String, Map<String, FileWriter>> next = iterator.next();
				Map<String, FileWriter> map = next.getValue();
				if (map != null) {
					Iterator<Entry<String, FileWriter>> iterator2 = map.entrySet().iterator();
					while(iterator2.hasNext()) {
						Entry<String, FileWriter> entry = iterator2.next();
						FileWriter fileWriter = entry.getValue();
						if (fileWriter != null) {
							fileWriter.destroy();
						}
					}
				}
			}
		}
	}

	@Override
	public synchronized void write(Object obj) throws IOException {
		if (obj instanceof AbstractSms) {
			dispatcher((AbstractSms) obj);
		} else {
			logger.error("参数类型错误!");
		}
	}

	@Override
	public synchronized void flush() throws IOException {
		
		if (store != null) {
			Iterator<Entry<String, Map<String, FileWriter>>> iterator = store.entrySet().iterator();
			while(iterator.hasNext()) {
				Entry<String, Map<String, FileWriter>> next = iterator.next();
				Map<String, FileWriter> map = next.getValue();
				if (map != null) {
					Iterator<Entry<String, FileWriter>> iterator2 = map.entrySet().iterator();
					while(iterator2.hasNext()) {
						Entry<String, FileWriter> entry = iterator2.next();
						FileWriter fileWriter = entry.getValue();
						if (fileWriter != null) {
							fileWriter.flush();
						}
					}
				}
			}
		}
	}

	public boolean isMultiModel() {
		return isMultiModel;
	}

	public void setMultiModel(boolean isMultiModel) {
		this.isMultiModel = isMultiModel;
	}
}
